package com.smartstate.IOC.schedul;


import com.smartstate.IOC.vo.DataVO;
import com.smartstate.common.web.httpResult.ResultVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class DataRealTimeRunnable implements Runnable{

    private List<DataVO> dataVOList;

    private String userId;

    ExecutorService executorService = Executors.newFixedThreadPool(10);

    public void setListAndUserId(List<DataVO> dataVOList, String userId){
        this.dataVOList = dataVOList;
        this.userId = userId;
    }

    @SneakyThrows
    @Override
    public void run() {
        List<Future> futureList = new ArrayList<>();
        Map<String, Object> mapAttribute = new HashMap<>();
        dataVOList.forEach((dataVO) ->{
            DataRealTimeRunnableChild runnableChild = new DataRealTimeRunnableChild(dataVO, userId);
            futureList.add(executorService.submit(runnableChild));
        });
        for (Future future : futureList) {
            Map<String, List<DataVO>> listMap = (Map<String, List<DataVO>>) future.get();
            mapAttribute.putAll(listMap);
        }
        WebSocketCopy.send(ResultVO.success(mapAttribute), userId);
    }
}
